<?php
/**
 * 用户相关
 *
 * @author  
 * @time 2024-01-23 14:06:36
 */

namespace App\Repositories;

use App\Exceptions\RepoException;
use App\Models\AiLawRbRobot;
use App\Models\AiLawUserResearchLog;
use App\Models\AiLawUserResearchTag;
use App\Models\AiLawUsers;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

class UserRepo
{

    /**
     * 更新用户信息
     *
     * @param $requestData
     * @return mixed
     * @author  
     * @time 2024-01-23 19:39:00
     */
    public function updateAILawUser($requestData)
    {

        $updateData = [
            'user_name' => $requestData['user_name'],
            'birthday' => $requestData['birthday'],
            'gender' => $requestData['gender'],
            'province' => $requestData['province'],
            'city' => $requestData['city'],
            'area' => $requestData['area'],
            'info' => $requestData['info'],
            'profile_photo' => $requestData['profile_photo_url'],
        ];

        $updateData = array_filter($updateData);

        AiLawUsers::query()->where("id", Auth::id())->update($updateData);

        return AiLawUsers::getAILawUser(Auth::id(), true);
    }


    /**
     * 保存调研信息
     *
     * @param $caseTypeIds
     * @param $question
     * @return void
     * @throws RepoException
     * @author  
     * @time 2024-01-24 15:50:28
     */
    public function saveUserResearch($caseTypeIds, $question)
    {
        $ids = AiLawRbRobot::query()
            ->whereIn('id', $caseTypeIds)
            ->pluck('id')->toArray();
        if (empty($ids)) {
            throw new RepoException("法务问题类型参数有误");
        }

        $userTag = [];
        foreach ($ids as $id) {
            $userTag[] = [
                'user_id' => Auth::id(),
                'case_type_id' => $id,
                'created_at' => Carbon::now(),
            ];
        }

        AiLawUserResearchTag::query()->insertOrIgnore($userTag);

        if (!empty($question)) {
            AiLawUserResearchLog::query()->create([
                'user_id' => Auth::id(),
                'question' => $question,
            ]);
        }

    }

}